package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

//目的地控制器
@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model) {

        //查询热门的区域,并且按sequence排序
        model.addAttribute("hotRegions", regionService.queryHotRegions());

        return "destination/index";
    }

    //根据区域id获取下面的目的地与子目的地
    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId) {
        List<Destination> dests = destinationService.getDestByRegionId(regionId);

        //左边
        List<Destination> leftDests = dests.subList(0, dests.size() / 2 + 1);
        model.addAttribute("leftDests", leftDests);

        //右边
        List<Destination> rightDests = dests.subList(dests.size() / 2 + 1, dests.size());
        model.addAttribute("rightDests", rightDests);

        //共享区域id
        model.addAttribute("regionId", regionId);

        return "destination/hotdestTpl";
    }


    //面包屑           指导
    @RequestMapping("/guide")         //该id是目的地的id
    public String guide(Model model, Long id) {

        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        model.addAttribute("toasts", toasts);

        //dest 拿到最后一个目的地
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);

        //查询 目的地 下的分类
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogsByDestId(id);
        model.addAttribute("catalogs", catalogs);

        //strategyDetails 广州攻略点击量前3的攻略集合
        model.addAttribute("strategyDetails",strategyDetailService.getViewnumTop3(id));

        return "destination/guide";
    }

    //纵览页
    @RequestMapping("/surveyPage")                                  // 1.qo
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        //2.toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts", toasts);

        //3.dest 拿到最后一个目的地
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest", dest);

        return "destination/survey";
    }

    //查询攻略内容
    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        //查询 目的地 下的分类 catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogsByDestId(qo.getDestId());
        model.addAttribute("catalogs", catalogs);

        //catalog
        if (catalogs.size() > 0) {
            for (StrategyCatalog catalog : catalogs) {
                if (catalog.getId() == qo.getCatalogId()) {
                    model.addAttribute("catalog", catalog);

                    List<StrategyDetail> details = catalog.getDetails();

                    //detail
                    if (details.size() > 0) {
                        StrategyDetail detail = details.get(0);
                        detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                        model.addAttribute("detail", detail);
                    }

                }
            }
        }

        return "destination/surveyTpl";
    }

    //游记相关的模板
    @RequestMapping("/travels")
    public String travels(Model model,@ModelAttribute("qo")TravelQuery qo){
        //分页查询
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";
    }
}
